package com.ziyucoding.springboot.labs.lab4.concurrentdemo.example.threadlocal;

/**
 * 请求绑定信息
 *
 * @author ziyucoding
 * @date 2022-11-27
 **/
public class RequestHolder {

    /**
     * 线程封闭
     * <p>
     * 线程封闭可用于数据库连接池（JDBC）
     */
    private final static ThreadLocal<Long> requestHolder = new ThreadLocal<>();

    public static void add(Long id) {
        requestHolder.set(id);
    }

    public static Long getId() {
        return requestHolder.get();
    }

    public static void remove() {
        // 记得移除
        requestHolder.remove();
    }

}
